/*
 * 文件名: MySpinner.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-XIAOFANQING001
 * 创建时间: 2011-12-21
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package app.widget;

import android.app.Dialog;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.longevityassistant.activity.R;

/**
 * 自定义的Spinner控件
 * @author EX-XIAOFANQING001
 * @date 2011-12-21
 * @version [Android PABank C01, @2011-12-21]
 * @description
 */
public class MySpinner extends LinearLayout
{
    /**
     * Spinner中显示的文本
     */
    private TextView tvContentLabel;
    
    /**
     * 列表适配器
     */
    private BaseAdapter mAdapter;
    
    /**
     * 弹出的下拉框
     */
    private MySpinnerDialog mySpinnerDialog;
    
    /**
     * 选择监听器
     */
    private OnItemSelectedListener onItemSelectedListener;
    
    public MySpinner(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    public MySpinner(Context context)
    {
        super(context);
        init();
    }
    
    /**
     * 初始化函数
     * [一句话功能简述]<BR>
     * [功能详细描述]
     */
    private void init() {
        setGravity(Gravity.CENTER);
        
        tvContentLabel = new TextView(getContext());
        tvContentLabel.setTextSize(16);
        tvContentLabel.setSingleLine();
        tvContentLabel.setTextColor(getResources().getColor(R.color.col_pa03));
        
        LayoutParams tvLp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        tvContentLabel.setLayoutParams(tvLp);
        addView(tvContentLabel);
        
        mySpinnerDialog = new MySpinnerDialog(getContext());
        
        setClickable(true);
        setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO 显示下拉菜单
                showPopupList();
            }
        });
    }
    
    /**
     * 设置Spinner的文本
     * [一句话功能简述]<BR>
     * [功能详细描述]
     * @param text
     */
    public void setText(String text) 
    {
        tvContentLabel.setText(text);
    }
    
    /**
     * 设置Spinner的文字颜色
     * [一句话功能简述]<BR>
     * [功能详细描述]
     * @param color
     */
    public void setTextColor(int color) 
    {
        tvContentLabel.setTextColor(color);
    }
    
    /**
     * 设置弹出数据的Adapter
     * [一句话功能简述]<BR>
     * [功能详细描述]
     * @param adapter
     */
    public void setAdapter(BaseAdapter adapter) 
    {
        mAdapter = adapter;
    }
    
    /**
     * 弹出对话框
     * [一句话功能简述]<BR>
     * [功能详细描述]
     */
    private void showPopupList() 
    {
        int[] pos = new int[2];
        getLocationOnScreen(pos);
        
        int x = pos[0];
        int y = pos[1] + getHeight();
        mySpinnerDialog.show(x, y);
    }
    
    /**
     * 设置选择监听器
     * [一句话功能简述]<BR>
     * [功能详细描述]
     * @param onItemSelectedListener
     */
    public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener)
    {
        this.onItemSelectedListener = onItemSelectedListener;
    }

    /**
     * 选择监听器接口
     * [一句话功能简述]<BR>
     * [功能详细描述]
     * @author EX-XIAOFANQING001
     * @version [Android PABank C01, 2011-12-22]
     */
    public interface OnItemSelectedListener {
        public void onItemSelected(int position);
    }
    
    /**
     * 弹出框对象
     * [一句话功能简述]<BR>
     * [功能详细描述]
     * @author EX-XIAOFANQING001
     * @version [Android PABank C01, 2011-12-22]
     */
    class MySpinnerDialog extends Dialog {

        private ListView lvSpinner;
        
        public MySpinnerDialog(Context context)
        {
            super(context, android.R.style.Theme_Translucent_NoTitleBar);
            
            lvSpinner = new ListView(context);
            
            lvSpinner.setDivider(null);
            lvSpinner.setCacheColorHint(0x00000000);
            
            super.setContentView(lvSpinner);
            
            lvSpinner.setOnItemClickListener(new OnItemClickListener() 
            {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                {
                    setText(mAdapter.getItem(position).toString());
                    dismiss();
                    if (onItemSelectedListener != null) {
                        onItemSelectedListener.onItemSelected(position);
                    }
                }
            });
          
        }
        
        /**
         * 在指定的位置弹出框
         * [一句话功能简述]<BR>
         * [功能详细描述]
         * @param x
         * @param y
         */
        public void show(int x, int  y) {
            lvSpinner.setAdapter(mAdapter);
            Window window = getWindow();
            window.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.y = y;
            
            window.setLayout(MySpinner.this.getWidth(), LayoutParams.WRAP_CONTENT);
            
            super.show();
        }

        /**
         * 触摸屏幕的任意位置让弹出框消失
         * [一句话功能简述]<BR>
         * [功能详细描述]
         * @param event
         * @return
         * @see android.app.Dialog#onTouchEvent(android.view.MotionEvent)
         */
        @Override
        public boolean onTouchEvent(MotionEvent event)
        {
            dismiss();
            return super.onTouchEvent(event);
        }
    }

}

